package com.test.web.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.he.core.ResultT;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * @author heyanjing
 * date:2020-06-09 2020/6/9:22:54
 */
@Slf4j
@AllArgsConstructor
@RestController
public class FileController {

    @PostMapping("/file/upload")
    public ResultT upload(@RequestPart MultipartFile file) {
        log.info("{}", file.getOriginalFilename());
        int i = 1 / 0;
        return ResultT.success();
    }

    @GetMapping("/file/download")
    public ResultT download(HttpServletResponse response) {
        File file = new File("D:\\Temp\\1.jpg");
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            IoUtil.copy(FileUtil.getInputStream(file), outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultT.success();
    }

}
